﻿using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;
using VitCode.Web.UI.WebControls;

namespace CRS.Web.BackStageManagement
{
    public partial class UserUpdate : VcPage
    {

        
        
        BaseDataManager m_Help = new BaseDataManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(
                "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            if (!IsPostBack)
            {
                DropListBind();
                if (!String.IsNullOrEmpty(GenericContext.CurrUserCode))
                {
                    if (Request["opt"] == "Edit")
                    {
                        trPassword.Visible = false;
                        trCheckPassword.Visible = false;
                        if (Request.QueryString["Id"] != null)
                        {
                            lableId.Text = Request.QueryString["Id"].Trim();

                            User currentUser;

                            IList objlist = m_Help.GetObjectList(new User().GetType().Name, "Id", lableId.Text.Trim());
                            if (objlist != null && objlist.Count > 0)
                            {
                                currentUser = (User) objlist[0];

                                ZLTextBox_Code.Text = currentUser.Code;
                                ZLTextBox_Number.Text = currentUser.Number;
                                ZLTextBox_Mobile.Text = currentUser.Mobile;
                                ZLTextBox_Email.Text = currentUser.Email;
                                ZLTextBox_AdminRealName.Text = currentUser.Name;
                                if (currentUser.Contact != null)
                                {
                                    string id = currentUser.Contact.Id.ToString().Trim();
                                    if (DropDownList_UserGroup.Items.Count > 0 &&
                                        DropDownList_UserGroup.Items.FindByValue(id) != null)
                                    {
                                        DropDownList_UserGroup.Items.FindByValue(id).Selected = true;
                                    }

                                }
                                else
                                {
                                    DropDownList_UserGroup.Items.FindByValue("").Selected = true;
                                }

                                IList objList2 = m_Help.GetObjectList(new UserRoleRelation().GetType().Name, "UserId",
                                                                      lableId.Text);
                                if (objList2 != null && objList2.Count > 0)
                                {
                                    var currentRoleRelation = (UserRoleRelation) objList2[0];
                                    if (currentRoleRelation.RoleId.ToString().Length == 36)
                                    {
                                        Guid guidRoleId = currentRoleRelation.RoleId;
                                        if (DropDownList_Role.Items.Count > 0 &&
                                            DropDownList_Role.Items.FindByValue(guidRoleId.ToString()) != null)
                                        {
                                            DropDownList_Role.Items.FindByValue(guidRoleId.ToString()).Selected = true;
                                        }

                                    }
                                }
                                else
                                {
                                    DropDownList_Role.Items.FindByValue("").Selected = true;
                                }
                            }
                        }
                    }
                    else
                    {
                        trPassword.Visible = true;
                        trCheckPassword.Visible = true;
                    }
                }
                else
                {
                    Response.Redirect("../Default.aspx");
                }
            }
        }

        //初始化下拉列表
        private void DropListBind()
        {
            DropDownList_UserGroup.Items.Add(new ListItem("", ""));
            DropDownList_Role.Items.Add(new ListItem("", ""));

            string sql = "select Id,Name from VitCode_Castle_ContactInfo";
            DataTable dt = m_Help.GetDataTableBySql(sql);

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    DropDownList_UserGroup.DataSource = dt;
                    DropDownList_UserGroup.DataTextField = "Name";
                    DropDownList_UserGroup.DataValueField = "Id";
                    DropDownList_UserGroup.DataBind();
                    DropDownList_UserGroup.Items.Insert(0,new ListItem("", ""));
                    dt.Dispose();
                }
            }
            
            sql = "select Id,Name from VitCode_Castle_Role";
            DataTable dt2 = m_Help.GetDataTableBySql(sql);
            if (dt2 != null)
            {
                if (dt2.Rows.Count > 0)
                {
                    DropDownList_Role.DataSource = dt2;
                    DropDownList_Role.DataTextField = "Name";
                    DropDownList_Role.DataValueField = "Id";
                    DropDownList_Role.DataBind();
                    DropDownList_Role.Items.Insert(0, new ListItem("", ""));
                    dt2.Dispose();
                }
            }
        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {

            Response.Redirect("UserList.aspx", false);
        }

        protected void Button_Update_Click(object sender, EventArgs e)
        {
            if (Request["opt"] == "Edit")
            {
                bool access;
                string strCode = ZLTextBox_Code.Text;
                string strNumber = ZLTextBox_Number.Text;
                string strMobile = ZLTextBox_Mobile.Text;
                string strEmail = ZLTextBox_Email.Text;
                string strName = ZLTextBox_AdminRealName.Text;

                string hql = " from User where Code ='" + ZLTextBox_Code.Text.Trim() + "' and Id !='" +
                             lableId.Text.Trim() + "'";
                IList userList = m_Help.Session.CreateQuery(hql).List();
                if (userList != null && userList.Count > 0)
                {
                    Alert.MessageBox("用户编号已存在！");
                    return;
                }
                User currentUser;
                IList objlist = m_Help.GetObjectList(new User().GetType().Name, "Id", lableId.Text.Trim());
                if (objlist != null && objlist.Count > 0)
                {
                    currentUser = (User) objlist[0];
                    currentUser.Name = strName;
                    currentUser.Mobile = strMobile;
                    currentUser.Email = strEmail;
                    currentUser.Number = strNumber;
                    currentUser.Code = strCode;

                    if (DropDownList_UserGroup.SelectedItem.Text.Trim().Length > 0)
                    {
                        string strUserGroup = DropDownList_UserGroup.SelectedItem.Value.Trim();

                        IList objList = m_Help.GetObjectList(new ContactInfo().GetType().Name,
                                                             "Id",
                                                             strUserGroup);
                        if (objList != null && objList.Count > 0)
                        {
                            var currentUserGroup =
                                (ContactInfo)objList[0];
                            currentUser.Contact = currentUserGroup;

                        }
                    }
                    if (DropDownList_Role.SelectedItem.Text.Trim().Length > 0)
                    {
                        Guid guidRoleId = new Guid(DropDownList_Role.SelectedItem.Value.Trim());
                        IList objList = m_Help.GetObjectList(new UserRoleRelation().GetType().Name, "UserId",
                                                             lableId.Text);
                        if (objList != null && objList.Count > 0)
                        {
                            var currentRoleRelation = (UserRoleRelation) objList[0];
                            currentRoleRelation.RoleId = guidRoleId;
                            access = m_Help.SaveOrUpdateObj(currentRoleRelation);
                        }
                        else
                        {
                            var currentRoleRelation = new UserRoleRelation
                                                          {
                                                              RoleId = guidRoleId,
                                                              UserId = new Guid(lableId.Text.Trim())
                                                          };
                            access = m_Help.SaveOrUpdateObj(currentRoleRelation);
                        }
                    }
                    else
                    {
                        IList objList = m_Help.GetObjectList(new UserRoleRelation().GetType().Name, "UserId",
                                                             lableId.Text);
                        if (objList != null && objList.Count > 0)
                        {
                            var currentRoleRelation = (UserRoleRelation) objList[0];
                            m_Help.DeleteObj(currentRoleRelation);
                        }
                    }
                    access = m_Help.SaveOrUpdateObj(currentUser);
                    if (access)
                        Page.RegisterClientScriptBlock("信息提示", "<script language='javascript'>alert('保存成功！')</script>");
                }
            }
            else
            {
                var objUser = new User();
                IList userList = m_Help.GetObjectList(new User().GetType().Name, "Code", ZLTextBox_Code.Text.Trim());
                if (userList != null && userList.Count > 0)
                {
                    Alert.MessageBox("用户编号已存在！");
                    return;
                }
                objUser.Code = ZLTextBox_Code.Text.Trim();
                objUser.Name = ZLTextBox_AdminRealName.Text.Trim();
                objUser.Mobile = ZLTextBox_Mobile.Text.Trim();
                objUser.Email = ZLTextBox_Email.Text.Trim();
                objUser.Number = ZLTextBox_Number.Text.Trim();
                if (ZLTextBox_AdminPass.Text.Trim().Length > 0)
                {
                    string StrPassword = publ.VitEnctyptString(ZLTextBox_AdminPass.Text.Trim());
                    objUser.Password = StrPassword;
                }
                else
                {
                    objUser.Password = publ.VitEnctyptString("123456");
                }
                if (DropDownList_UserGroup.Items.Count > 0)
                {
                    if (DropDownList_UserGroup.SelectedItem.Text.Trim().Length > 0)
                    {
                        string strUserGroup = DropDownList_UserGroup.SelectedItem.Value.Trim();

                        IList objList = m_Help.GetObjectList(new ContactInfo().GetType().Name,
                                                             "Id", strUserGroup);
                        if (objList != null && objList.Count > 0)
                        {
                            var currentUserGroup = (ContactInfo)objList[0];
                            objUser.Contact = currentUserGroup;

                        }
                    }

                }
                bool access = m_Help.SaveOrUpdateObj(objUser);
                if (DropDownList_Role.Items.Count > 0)
                {
                    if (DropDownList_Role.SelectedItem.Text.Trim().Length > 0)
                    {
                        Guid guidRoleId = new Guid(DropDownList_Role.SelectedItem.Value.Trim());
                        IList objList = m_Help.GetObjectList(new User().GetType().Name, "Code", ZLTextBox_Code.Text);
                        if (objList != null && objList.Count > 0)
                        {
                            var currentUser = (User) objList[0];
                            Guid guidUserId = currentUser.Id;
                            var currentUserRoleRelation = new UserRoleRelation();
                            currentUserRoleRelation.RoleId = guidRoleId;
                            currentUserRoleRelation.UserId = guidUserId;
                            access = m_Help.SaveOrUpdateObj(currentUserRoleRelation);
                        }

                    }
                }

                if (access)
                    Page.RegisterClientScriptBlock("信息提示", "<script language='javascript'>alert('保存成功！')</script>");

            }
        }

        protected void Select_Click(object sender, EventArgs e)
        {
            Window win = new Window(Page, "角色选择");
            win.Height = 400;
            win.Width = 600;
            win.Scrollbars = true;
            win.Status = true;
            win.ShowModalDialog("RoleList.aspx", new string[] { "hdCheckInfo" });

        }
    }
}
